﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NPlot;

namespace Uptime_Viewer
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }



        private void Form1_Load(object sender, EventArgs e)
        {
            string text;
            text = System.IO.File.ReadAllText(@"C:\RouterLog.txt");
            
            string[] splitText = text.Split(new string[]{"Trafic: ","\r"},System.StringSplitOptions.RemoveEmptyEntries);
            List<decimal> data = new List<decimal>();
            List<DateTime> date = new List<DateTime>();

            foreach (string i in splitText)
            {
                if (i.Contains(" / "))
                {
                    int startIndex = i.IndexOf("/ ") + 2;
                    int letters = i.Length - startIndex;
                    decimal final = decimal.Parse(i.Substring(startIndex, letters));

                    data.Add(final);
                }
                else if(i.Length > 10)
                {
                    string StringTime = i;
                    int stopIndex = StringTime.IndexOf(" - ") - 1;
                    StringTime = StringTime.Substring(0, stopIndex);

                    StringTime = StringTime.Replace("-", " ");
                    StringTime = StringTime.Insert(0, "2008-01-");
                    
                    DateTime newDate = DateTime.Parse(StringTime);

                    date.Add(newDate);
                }
            }

            MakeChart(date, data);

            
        }

        private void MakeChart(List<DateTime> date,List<decimal> data)
        {
            MainPlot.Clear();

            //Add a background grid for better chart readability.
            Grid grid = new Grid();
            grid.VerticalGridType = Grid.GridType.Coarse;
            grid.HorizontalGridType = Grid.GridType.Fine; ;
            grid.MajorGridPen = new Pen(Color.LightGray, 1.0f);

            MainPlot.Add(grid);

            //Create a step plot instance for the balance chart.
            LinePlot stepBalance = new LinePlot();
            stepBalance.Pen = new Pen(Color.DarkOrange, 2);


            //Create the lists from which to pull data.
            List<decimal> dataAmount = new List<decimal>();


            //Set the datasource and the xax for the balance plot.
            stepBalance.AbscissaData = date;
            stepBalance.DataSource = data;

            //Add stepBalance to plotSurfaceBalance.
            MainPlot.Add(stepBalance);

            //MainPlot general settings.
            MainPlot.ShowCoordinates = true;
            MainPlot.YAxis1.Label = "Data Used (G)";
            MainPlot.YAxis1.LabelOffsetAbsolute = true;
            MainPlot.YAxis1.LabelOffset = 40;
            MainPlot.Padding = 5;
            

            //Refresh surfaces.
            MainPlot.Refresh();

        }
    }
}
